package com.example.chongwushangcheng;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageButton;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.chongwushangcheng.adapter.PetExchangeAdapter;
import com.example.chongwushangcheng.model.PetExchangePost;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

public class PetExchangeActivity extends AppCompatActivity {
    
    private RecyclerView rvPosts;
    private PetExchangeAdapter adapter;
    private List<PetExchangePost> postList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pet_exchange);
        
        // 初始化返回按钮
        ImageButton btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(v -> finish());
        
        // 初始化发布按钮
        FloatingActionButton fabPublish = findViewById(R.id.fab_publish);
        fabPublish.setOnClickListener(v -> {
            // 跳转到发布页面
            Intent intent = new Intent(PetExchangeActivity.this, PublishActivity.class);
            startActivity(intent);
        });
        
        // 初始化帖子列表
        initPostList();
    }
    
    private void initPostList() {
        rvPosts = findViewById(R.id.rv_posts);
        rvPosts.setLayoutManager(new LinearLayoutManager(this));
        
        // 创建帖子数据
        postList = createPostData();
        
        // 设置适配器
        adapter = new PetExchangeAdapter(this, postList);
        rvPosts.setAdapter(adapter);
        
        // 设置点击事件，跳转到详情页
        adapter.setOnItemClickListener(position -> {
            Intent intent = new Intent(PetExchangeActivity.this, PetExchangeDetailActivity.class);
            // 可以传递一些数据到详情页
            // intent.putExtra("post_id", postList.get(position).getId());
            startActivity(intent);
        });
    }
    
    private List<PetExchangePost> createPostData() {
        List<PetExchangePost> posts = new ArrayList<>();
        // 添加一些示例数据
        posts.add(new PetExchangePost(1, "求助:两个月比熊一顿喂食量该多少呢？", "刚到家几天的小比熊，目前2个月2天，第一次养狗，有没有人知道该喂多少粮呢？", "2025-07-05", "济南万象城", R.drawable.pet_2));
        posts.add(new PetExchangePost(2, "猫咪总喜欢咬人怎么办？", "家里3个月大的小猫，太调皮了，总是喜欢咬人，求解决办法！", "2025-07-04", "北京朝阳", R.drawable.pet_2));
        posts.add(new PetExchangePost(3, "推荐一款好用的宠物梳子", "家里的金毛掉毛太严重了，有没有推荐的好用梳子？", "2025-07-03", "上海浦东", R.drawable.pet_2));
        posts.add(new PetExchangePost(4, "寻找宠物医院", "家附近有什么靠谱的宠物医院推荐吗？", "2025-07-02", "广州天河", R.drawable.pet_2));
        posts.add(new PetExchangePost(5, "宠物保险值得买吗？", "想给家里的狗狗买保险，有没有推荐的保险公司和套餐？", "2025-07-01", "深圳南山", R.drawable.pet_2));
        return posts;
    }
} 